/*
 * @Description  : 环境相关配置
 * @LastEditTime : 2024-06-27 09:13:30
 */

import { getUrlParams } from "@/utils/utils"
let appid = ""
// #ifdef MP-WEIXIN
const accountInfo = uni.getAccountInfoSync()
appid = accountInfo.miniProgram?.appId
// #endif
const { platform, uniPlatform } = uni.getSystemInfoSync()

// 当前环境
export const env = uniPlatform === "mp-weixin" ? uni.getAccountInfoSync().miniProgram.envVersion : process.env.UNI_WEB_ENV === "test" ? "test" : process.env.NODE_ENV

// 环境配置
export const envs = {
  // 微信开发环境
  develop: {
    env: "develop",
    api: genApi("develop"),
    account: genAccount("develop"),
    password: genPassword("develop")
  },
  // 微信体验环境
  trial: {
    env: "trial",
    api: genApi("trial"),
    account: genAccount("trial"),
    password: genPassword("trial")
  },
  // 微信正式环境
  release: {
    env: "release",
    api: genApi("release"),
    account: genAccount("release"),
    password: genPassword("release")
  },
  // 开发环境
  development: {
    env: "development",
    api: genApi("development"),
    account: genAccount("development"),
    password: genPassword("development")
  },
  // 测试环境(WEB环境存在)
  test: {
    env: "test",
    api: genApi("test"),
    account: genAccount("test"),
    password: genPassword("test")
  },
  // 正式环境
  production: {
    env: "production",
    api: genApi("production"),
    account: genAccount("production"),
    password: genPassword("production")
  }
}

function genApi(env) {
  switch (env) {
    // 微信体验环境
    case "trial":
      // 欢银
      if (appid == "wx0eb93ef437281515") {
        return "https://mapi.zrfilm.com/api"
      } else {
        // 最美
        return "https://ilike-cloud-mapi.zmaxfilm.com/api"
      }
    // 微信正式环境
    case "release":
      // 欢银
      if (appid == "wx0eb93ef437281515") {
        return "https://mapi.zrfilm.com/api"
      } else {
        // 最美
        return "https://ilike-cloud-mapi.zmaxfilm.com/api"
      }
    // 微信开发环境
    case "develop":
      return "https://tkzc-release-napi.zmaxfilm.com/api"
    // 开发环境
    case "development":
      // web平台
      if (uniPlatform === "web") {
        const proxy = getUrlParams()
        return proxy.apiEnv ? `${proxy.apiEnv}/api` : "/dev/api"
      } else {
        return "https://tkzc-release-napi.zmaxfilm.com/api"
      }
    // 测试环境
    case "test":
      return "https://tkzc-release-napi.zmaxfilm.com/api"
    // 正式环境
    case "production":
      // web平台
      if (uniPlatform === "web" && process.env.NODE_ENV === "development") {
        let url = "/prod/api"
        if (process.env.UNI_WEB_ENV === "production") url = "https://ilike-cloud-mapi.zmaxfilm.com/api"
        return url
      } else {
        let url = "https://ilike-cloud-mapi.zmaxfilm.com/api"
        if (process.env.UNI_WEB_ENV === "test") url = "https://tkzc-release-napi.zmaxfilm.com/api"
        return url
      }
    default:
      return "https://ilike-cloud-mapi.zmaxfilm.com/api"
  }
}

function genAccount(env) {
  switch (env) {
    // 微信体验环境
    case "trial":
      // 欢银
      if (appid == "wx0eb93ef437281515") {
        return "tkzc_ytbWMP"
      } else {
        // 最美
        return "zuimei_xcx"
      }
    // 微信正式环境
    case "release":
      // 欢银
      if (appid == "wx0eb93ef437281515") {
        return "tkzc_ytbWMP"
      } else {
        // 最美
        return "zuimei_xcx"
      }
    // 微信开发环境
    case "develop":
      // 欢银
      if (appid == "wx0eb93ef437281515") {
        return "tkzc_ytbWMP"
      } else {
        // 最美
        return "mp_weixin_test"
      }
    // 测试环境
    case "test":
      return "H5_test"
    // 开发环境
    case "development":
      if (uniPlatform === "web") {
        const proxy = getUrlParams()
        let account = "H5_test"
        if (proxy.apiEnv === "prod") account = "zuimei"
        return account
      } else if (uniPlatform === "app") {
        return platform === "ios" ? "ios_test" : "android_test"
      } else {
        return "H5_test"
      }
    // 正式环境
    case "production":
      if (uniPlatform === "web") {
        let account = "zuimei"
        if (process.env.UNI_WEB_ENV === "test") account = "H5_test"
        return account
      } else if (uniPlatform === "app") {
        return platform === "ios" ? "zuimei_ios" : "zuimei_android"
      } else {
        return "zuimei"
      }
    default:
      return "mp_weixin_test"
  }
}

function genPassword(env) {
  switch (env) {
    // 微信体验环境
    case "trial":
      // 欢银
      if (appid == "wx0eb93ef437281515") {
        return "07E6721A8C3C53C1"
      } else {
        // 最美
        return "zuimei520"
      }
    // 微信正式环境
    case "release":
      // 欢银
      if (appid == "wx0eb93ef437281515") {
        return "07E6721A8C3C53C1"
      } else {
        // 最美
        return "zuimei520"
      }
    // 微信开发环境
    case "develop":
      // 欢银
      if (appid == "wx0eb93ef437281515") {
        return "07E6721A8C3C53C1"
      } else {
        // 最美
        return "123456"
      }
    // 测试环境
    case "test":
      return "123456"
    // 开发环境
    case "development":
      if (uniPlatform === "web") {
        const proxy = getUrlParams()
        let password = "123456"
        if (proxy.apiEnv === "prod") password = "hjsdf231df2"
        return password
      } else if (uniPlatform === "app") {
        return platform === "ios" ? "123456" : "123456"
      } else {
        return "123456"
      }
    // 正式环境
    case "production":
      if (uniPlatform === "web") {
        let password = "hjsdf231df2"
        if (process.env.UNI_WEB_ENV === "test") password = "123456"
        return password
      } else if (uniPlatform === "app") {
        return platform === "ios" ? "zuimei_ios520" : "hjsdf231df2"
      } else {
        return "hjsdf231df2"
      }
    default:
      return "123456"
  }
}
